package com.eorchis.module.basedata.ui.controller;

import com.eorchis.core.servicetemplate.treetemplate.jsonbean.JsonTreeBean;
import com.eorchis.module.ModelBeanUtils;
import com.eorchis.module.basedata.domain.BaseDataType;
import com.eorchis.module.basedata.domain.BaseDataTypeCondition;
import com.eorchis.module.basedata.service.IBaseDataTypeService;
import com.eorchis.module.basedata.ui.commond.BaseDataTypeCommond;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("baseDataTypeController")
@RequestMapping({"/module/basedata"})
public class BaseDataTypeController
{
  public static final String modulePath = "/module/basedata";

  @Autowired
  @Qualifier("com.eorchis.module.basedata.service.impl.BaseDataTypeServiceImpl")
  private IBaseDataTypeService baseDataTypeService;

  @RequestMapping({"/findBaseDataType"})
  @ResponseBody
  public List<JsonTreeBean> findBaseDataType(BaseDataTypeCommond commond)
    throws Exception
  {
    BaseDataTypeCondition condition = new BaseDataTypeCondition();
    BeanUtils.copyProperties(condition, commond);
    if(condition.getNode().equals("1")){
    	condition.setNode("-2");
    }
    condition.setFindLeapNode(true);
    List list = this.baseDataTypeService.doProcess(condition);
    return list;
  }

  @RequestMapping({"/addBaseDataType"})
  public String addBaseDataType(Model model, HttpServletRequest request, BaseDataTypeCommond commond)
    throws Exception
  {
    String msg = "";
    BaseDataTypeCondition condition = new BaseDataTypeCondition();
    condition.setSearchTypeID(commond.getParentID());

    BaseDataType parentBase = this.baseDataTypeService.getBaseDataType(condition);
    if (parentBase.getTypeID().equals("-1"))
    {
      ModelBeanUtils.jsonObjectToModel(model, false, "数据字典节点下不能添加子节点", null);
      return "";
    }

    BaseDataType baseDataType = new BaseDataType();
    baseDataType.setParentID(commond.getParentID());
    baseDataType.setTypeCode(commond.getTypeCode());
    baseDataType.setTypeName(commond.getTypeName());
    baseDataType.setActiveState(BaseDataType.IS_ACTIVE_Y);
    if (this.baseDataTypeService.checkTypeCodeIsRepeat(baseDataType))
    {
      this.baseDataTypeService.addBaseType(baseDataType);

      ModelBeanUtils.jsonObjectToModel(model, true, "添加成功", baseDataType);
    } else {
      msg = "编码重复";
      ModelBeanUtils.jsonObjectToModel(model, false, msg, baseDataType);
    }

    return "";
  }

  @RequestMapping({"/preUpdateBaseDataType"})
  public String preUpdateBaseDataType(Model model, BaseDataTypeCommond commond)
    throws Exception
  {
    BaseDataTypeCondition condition = new BaseDataTypeCondition();
    condition.setSearchTypeID(commond.getTypeID());

    BaseDataType baseDataType = this.baseDataTypeService.getBaseDataType(condition);

    if (baseDataType.getTypeID().equals("-1")) {
      baseDataType = null;
      ModelBeanUtils.jsonObjectToModel(model, false, "数据字典不能修改", null);
    } else if (baseDataType.getTypeID().equals("2")) {
      baseDataType = null;
      ModelBeanUtils.jsonObjectToModel(model, false, "公共字典不能修改", null); } else {
      if (baseDataType.getTypeID().equals("3")) {
        baseDataType = null;
        ModelBeanUtils.jsonObjectToModel(model, false, "本系统字典不能修改", null);
        throw new Exception("测试");
      }
      ModelBeanUtils.jsonObjectToModel(model, true, null, baseDataType);
    }

    return "";
  }

  @RequestMapping({"/updateBaseDataType"})
  public String updateBaseDataType(Model model, HttpServletRequest request, BaseDataTypeCommond commond)
    throws Exception
  {
    BaseDataTypeCondition condition = new BaseDataTypeCondition();
    condition.setSearchTypeID(commond.getTypeID());

    BaseDataType baseDataType = this.baseDataTypeService.getBaseDataType(condition);
    BaseDataType temp = new BaseDataType();
    temp.setTypeID(commond.getTypeID());
    temp.setTypeCode(commond.getTypeCode());
    if (this.baseDataTypeService.checkTypeCodeIsRepeat(temp))
    {
      baseDataType.setTypeCode(commond.getTypeCode());
      baseDataType.setTypeName(commond.getTypeName());

      this.baseDataTypeService.updateBaseDataType(baseDataType);

      ModelBeanUtils.jsonObjectToModel(model, true, "修改成功", baseDataType);
    } else {
      ModelBeanUtils.jsonObjectToModel(model, false, "编码重复", baseDataType);
    }
    return "";
  }

  @RequestMapping({"/discardBaseDataType"})
  public String discardBaseDataType(Model model, BaseDataTypeCommond commond)
    throws Exception
  {
    BaseDataTypeCondition condition = new BaseDataTypeCondition();
    condition.setSearchTypeID(commond.getTypeID());
    condition.setNode(commond.getTypeID().toString());

    if (commond.getTypeID().equals("-1")) {
      ModelBeanUtils.jsonObjectToModel(model, false, "数据字典不能删除", null);
      return "";
    }if (commond.getTypeID().equals("2")) {
      ModelBeanUtils.jsonObjectToModel(model, false, "公共字典不能删除", null);
      return "";
    }if (commond.getTypeID().equals("3")) {
      ModelBeanUtils.jsonObjectToModel(model, false, "本系统字典不能删除", null);
      return "";
    }

    if (!this.baseDataTypeService.checkIsNotExistsDataOrDataType(condition)) {
      this.baseDataTypeService.discardOrReuseBaseDataType(condition);
      ModelBeanUtils.jsonObjectToModel(model, true, "删除成功", null);
    } else {
      ModelBeanUtils.jsonObjectToModel(model, false, "存在子信息或基础数据", null);
    }
    return "";
  }
}